<template>
	<view>
		<mescroll-body ref="mescrollRef" bottom="100rpx" @init="mescrollInit" @down="downCallback" @up="upCallback">
			<view class="py10 bg-fff mt10" v-for="(item, index) in pageList" :key="index" @tap="checkAddress(item)">
				<view class="px15 bb pb10">
					<view class="dis-flex">
						<view class="fs14 fwb c-333 ellipsis-1">
							{{item.truename}}
						</view>
						<view class="c-999 ml10 fwb fs14 flex">
							{{item.mobile}}
						</view>
					</view>
					<view class="dis-flex c-999 lh30 over mt8">
						<text class="fs14 ellipsis-2">{{item.region}}/{{item.city}}/{{item.xian}} {{item.address}}</text>
					</view>
					<!-- <view class="c-666 dis-flex over">
						<text class="ellipsis-2 fs15 fwb c-333">{{item.address}}</text>
					</view> -->
				</view>
				<view class="dis-flex c-333 mt10 flex-between flex-align-center mr15">
					<view v-if="item.status=='1'" class="bg-common fs14 ml15 br5 lh24">
						<text class="c-fff px10">默认地址</text>
					</view>
					<!-- <view class="scale8 fs14 ml5">
						<checkbox :checked="item.checked" @tap.stop="setDefaultAddress(item)">{{item.beDefault=="0"?"默认地址":"设为默认"}}</checkbox>
					</view> -->
					<view></view>
					<view class="dis-flex flex-align-center">
						<view class="dis-flex flex-center mr20" @tap.stop="deleteAddress(item.id)">
							<u-icon  size="45" color="#666" name="shanchu"  custom-prefix="custom-icon"></u-icon>
							<text class="c-666 fs13">删除</text>
						</view>
						<view class="dis-flex flex-center right " @tap.stop="addAddress(1,item)">
							<u-icon  size="30" color="#666" name="xiugai"  custom-prefix="custom-icon"></u-icon>
							<text class="c-666 fs13 ml5">修改</text>
						</view>
					</view>
					<!-- <button class="bd-common c-common btn-minis mr15" size="mini" style="background-color: #fff;" @tap.stop="deleteAddress(item.addressId)">删除</button> -->
				</view>
			</view>
		</mescroll-body>
		<view class="btn-submit-box">
			<button class="btn-submit bg-common c-fff" @tap="addAddress(0,null)">+新建地址</button>
		</view>
	</view>
</template>

<script>
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	
	export default {
		mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
		data() {
			return {
				pageList: [],
				source: 0,//来源，0个人中心，1下单
			}
		},
		
		onLoad(option){
			this.source = Number(option.source)||0;
		},
		
		methods: {
			downCallback(){
				setTimeout(() => {
					this.mescroll.resetUpScroll();
				},1000);
			},
			
			upCallback(page) {
				this.$http.post(this.$config.UrlGetAddressList,{
				},{log:true,header: {
						'content-type': 'application/json'
					}}).then((res) => {
					let datalist = res?.data || []
					if (page.num == 1) this.pageList = [];
					this.pageList.push.apply(this.pageList, datalist);
					this.mescroll.endBySize(datalist.length, datalist.length);
				}).catch((err) => {
					this.mescroll.endErr();
				})
			},
			
			//设置默认地址
			setDefaultAddress(item){
				this.$http.post(this.$config.UrlAddressDeafult,{
				    addressId:item.addressId,beDefault:0,
				},{
					header: {
						'content-type': 'application/json',
					}
				}).then((res)=>{
					if(res.success){
						this.pageList = [];
						this.mescroll.resetUpScroll();
					}else this.$api.toast(res.msg);
				})
			},
			
			
			//删除地址
			deleteAddress(id){
				this.$api.alertConfirm('确定要删除该地址吗？',()=>{
					this.$http.post(this.$config.UrlDeleteAddress,{
					      id:id,
					},{
						header:{
							'content-type': "application/json",
						}
					}).then((res)=>{
						if(res.code == 1){
							this.$api.toast('删除成功');
							this.mescroll.resetUpScroll();
						}else this.$api.toast(res.msg);
					})
				})
			},
			
			
			//选择地址
			checkAddress(item){
				if(this.source === 1){
					this.$api.opener().updateaddressInfo(item);
					uni.navigateBack();
				}
			},
			
			
			//添加地址
			addAddress(type, item){
				this.$api.openWindow('/pages/center/addressManager?type='+type+'&data='+encodeURIComponent(JSON.stringify(item)));
			},
			
			//刷新页面
			refreshList(){
                this.mescroll.resetUpScroll();
			}
		}
	}
</script>

<style scoped>
	.btn-minis{border-radius: 40rpx;height: 50rpx;;line-height: 50rpx;font-size: 26rpx;min-width: 100rpx;}
</style>
